package org.redemptionhost.web;

import java.util.logging.Logger;

import org.apache.wicket.authorization.strategies.page.SimplePageAuthorizationStrategy;
import org.apache.wicket.guice.GuiceComponentInjector;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.session.ISessionStore;
import org.redemptionhost.web.pages.Guestbook;
import org.redemptionhost.web.pages.HomePage;
import org.redemptionhost.web.pages.LoginPage;
import org.redemptionhost.web.pages.SecuredPage;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * Application object for your web application.
 */
public class RedemptionApplication extends WebApplication
{
	private static final Logger logger = Logger.getLogger(RedemptionApplication.class.getName());
	
	@Override
    public Class<HomePage> getHomePage()
    {
        return HomePage.class;
    }

    @Override
    protected void init()
    {
        super.init();

 //       logger.severe("In here");
        // for Google App Engine
        getResourceSettings().setResourcePollFrequency(null);

        // Enable Guice for field injection on Wicket pages.  Unfortunately, constructor injection into
        // pages is not supported.  Supplying ServletModule is optional; it enables usage of @RequestScoped and
        // @SessionScoped, which may not be useful for Wicket applications because the WebPage instances are
        // already stored in session, with their dependencies injected once per session.
        addComponentInstantiationListener(new GuiceComponentInjector(this, new GuiceModule()));
//        addComponentInstantiationListener(new GuiceComponentInjector(this, new ServletModule(), new GuiceModule()));
        
		SimplePageAuthorizationStrategy strategy = new SimplePageAuthorizationStrategy(SecuredPage.class, LoginPage.class) {
			@Override
			protected boolean isAuthorized() {
				final UserService userService = UserServiceFactory.getUserService();
				return (userService.isUserLoggedIn());
			}
		};
		getSecuritySettings().setAuthorizationStrategy(strategy);
    }

    @Override
    protected ISessionStore newSessionStore()
    {
        // for Google App Engine
        return new HttpSessionStore(this);
    }
}
